﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;

namespace Kokomo.Controls
{
	public class TabbedDocumentBase : Selector
	{
		public TabbedDocumentBase()
		{
			this.GetType().GetProperty("CanSelectMultiple", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Public).SetValue(this, false, null);
		}

		#region Templating
		#region ContentTemplateSelector
		public DataTemplateSelector ContentTemplateSelector
		{
			get { return (DataTemplateSelector)GetValue(ContentTemplateSelectorProperty); }
			set { SetValue(ContentTemplateSelectorProperty, value); }
		}

		// Using a DependencyProperty as the backing store for ContentTemplateSelector.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ContentTemplateSelectorProperty =
			ContentControl.ContentTemplateSelectorProperty.AddOwner(typeof(TabbedDocumentBase));
		#endregion
		#region ContentTemplate
		public DataTemplate ContentTemplate
		{
			get { return (DataTemplate)GetValue(ContentTemplateProperty); }
			set { SetValue(ContentTemplateProperty, value); }
		}

		// Using a DependencyProperty as the backing store for ContentTemplateSelector.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ContentTemplateProperty =
			ContentControl.ContentTemplateProperty.AddOwner(typeof(TabbedDocumentBase));
		#endregion
		#region ContentStringFormat
		public string ContentStringFormat
		{
			get { return (string)GetValue(ContentStringFormatProperty); }
			set { SetValue(ContentStringFormatProperty, value); }
		}

		// Using a DependencyProperty as the backing store for ContentTemplateSelector.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty ContentStringFormatProperty =
			ContentControl.ContentStringFormatProperty.AddOwner(typeof(TabbedDocumentBase));
		#endregion

		#endregion

		#region VisualTree
		public static ContentPresenter GetVisualTree(DependencyObject obj)
		{
			return (ContentPresenter)obj.GetValue(VisualTreeProperty);
		}

		public static void SetVisualTree(DependencyObject obj, ContentPresenter value)
		{
			obj.SetValue(VisualTreeProperty, value);
		}

		// Using a DependencyProperty as the backing store for VisualTree.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty VisualTreeProperty =
			DependencyProperty.RegisterAttached("VisualTree", typeof(ContentPresenter), typeof(TabbedDocumentBase), new UIPropertyMetadata(null));

		protected ContentPresenter GetVisualTreeForItem(DependencyObject itemContainer)
		{
			if (itemContainer == null) throw new ArgumentNullException("itemContainer");

			ContentPresenter visualTree = GetVisualTree(itemContainer);
			if (visualTree != null) return visualTree;

			DataTemplate contentTemplate = this.ContentTemplate;
			DataTemplateSelector contentTemplateSelector = this.ContentTemplateSelector;
			string contentStringFormat = this.ContentStringFormat;
			bool hasLocalContentTemplate =
				(contentTemplate != null)
				|| (contentTemplateSelector != null)
				|| (contentStringFormat != null)
				;
			if (!hasLocalContentTemplate)
			{
				TabbedDocumentBase parent = this;
				if (parent != null)
				{
					contentTemplate = parent.ContentTemplate;
					contentTemplateSelector = parent.ContentTemplateSelector;
					contentStringFormat = parent.ContentStringFormat;
				}
			}

			visualTree = new ContentPresenter();
			visualTree.SetBinding(ContentPresenter.ContentProperty, new Binding() { Source = itemContainer, Path = new PropertyPath(FrameworkElement.DataContextProperty.Name) });
			visualTree.ContentTemplate = contentTemplate;
			visualTree.ContentTemplateSelector = contentTemplateSelector;
			visualTree.ContentStringFormat = contentStringFormat;
			//presenter.SetBinding(ContentPresenter.ContentTemplateProperty, new Binding() { Source = this, Path = new PropertyPath(ContentTemplateProperty.Name) });
			//presenter.SetBinding(ContentPresenter.ContentTemplateSelectorProperty, new Binding() { Source = this, Path = new PropertyPath(ContentTemplateSelectorProperty.Name) });
			//presenter.SetBinding(ContentPresenter.ContentStringFormatProperty, new Binding() { Source = this, Path = new PropertyPath(ContentStringFormatProperty.Name) });
			SetVisualTree(itemContainer, visualTree);

			return visualTree;
		}
		#endregion

		#region Items and containers
		private DocumentTab FindFirstContainer()
		{
			DocumentTab firstDockedItem = null;
			foreach (var item in this.Items)
			{
				DocumentTab container = this.ItemContainerGenerator.ContainerFromItem(item) as DocumentTab;
				if (container != null)
				{
					firstDockedItem = container;
					break;
				}
			}

			return firstDockedItem;
		}
		#endregion

		#region Selection
		protected bool EnsureSelectionIfPossible()
		{
			if (this.SelectedItem == null)
			{
				this.SelectedItem = this.FindFirstContainer();
				return true;
			}

			return false;
		}
		protected override void OnSelectionChanged(SelectionChangedEventArgs e)
		{
			base.OnSelectionChanged(e);

			object selectedItem = this.SelectedItem;
			if (selectedItem == null)
			{
				this.EnsureSelectionIfPossible();
			}
		}
		#endregion
	}
}
